package com.juli.school.service.impl;

import java.util.List;

import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.google.common.collect.Lists;
import com.juli.school.dao.entity.Point;
import com.juli.school.dao.mapper.PointMapper;
import com.juli.school.service.bo.PointBo;
import com.juli.school.service.utils.BeanUtils;
import com.juli.school.service.utils.DateUtil;
import com.juli.school.utils.core.model.CommonResp;
import com.juli.school.utils.core.model.PageResp;
import com.juli.school.utils.dao.base.Page;

/**
 * @Description: 积分变动表
 * @author: wjp
 * @date: 2018年1月29日下午10:19:51
 */
@Service
public class PointService {

	private static final Logger logger = LoggerFactory.getLogger(PointService.class);

	@Resource
	PointMapper pointMapper;

	/**
	 * @Description: 新增一条积分变动，如果是减少积分请用负数数字表示例如：扣除1分则，score = -1
	 * @author: wjp
	 * @date: 2018年1月29日下午10:40:06
	 */
	public CommonResp<String> add(Point point) {
		CommonResp<String> commonResp = new CommonResp<String>();
		try {
			if (StringUtils.isBlank(point.getStuId())) {
				commonResp.fail("入参stuId不能为空");
				return commonResp;
			}
			if (point.getScore() < 0) {// 如果是扣除积分，则去验证指定学生积分扣除时，是否足够
				Long sumBack = pointMapper.getSumBack(point.getStuId());
				if (sumBack.intValue() + point.getScore().intValue() < 0) {
					commonResp.fail("扣除学生分值不够本次扣除，本次分值："+point.getScore());
					return commonResp;
				}
			}
			point.setUpTime(DateUtil.getDateTime("yyyy-MM-dd hh:mm:ss"));
			pointMapper.insert(point);
			commonResp.success();
		} catch (Exception e) {
			logger.error("新增积分变动信息异常", e);
			commonResp.exception("新增积分变动信息异常" + e.getMessage());
		}
		return commonResp;
	}

	public CommonResp<String> update(Point point) {
		CommonResp<String> commonResp = new CommonResp<String>();
		try {
			if (StringUtils.isBlank(point.getStuId())) {
				commonResp.fail("入参stuId不能为空");
				return commonResp;
			}
			pointMapper.update(point);
			commonResp.success();
		} catch (Exception e) {
			logger.error("更新积分变动信息异常", e);
			commonResp.exception("更新积分变动信息异常" + e.getMessage());
		}
		return commonResp;
	}

	public CommonResp<PointBo> getPointsSum(String stuid) {
		CommonResp<PointBo> commonResp = new CommonResp<PointBo>();
		try {
			if (StringUtils.isBlank(stuid)) {
				commonResp.fail("stuId不可为空");
				return commonResp;
			}
			Long sumBack = pointMapper.getSumBack(stuid);
			PointBo pointBo = new PointBo();
			pointBo.setStuId(stuid);
			pointBo.setSumBack(sumBack);
			commonResp.setData(pointBo);
			commonResp.success();
		} catch (Exception e) {
			logger.error("获取学生积分发生异常", e);
			commonResp.exception("获取学生积分发生异常" + e.getCause());
		}
		return commonResp;
	}

	public CommonResp<PageResp<PointBo>> listPage(Point point, Integer pageNo, Integer pageSize) {
		CommonResp<PageResp<PointBo>> commonResp = new CommonResp<PageResp<PointBo>>();
		try {
			if (StringUtils.isBlank(point.getStuId())) {
				commonResp.fail("stuId不可为空");
				return commonResp;
			}
			// 设置分页信息
			Page page = new Page();
			page.setCurrentPage(pageNo == null ? 0 : pageNo);
			page.setShowCount(pageSize == null ? 10 : pageSize);
			point.setPage(page);
			List<Point> listPage = pointMapper.listPage(point);
			List<PointBo> list = Lists.newArrayList();
			for (Point p : listPage) {
				PointBo pointBo = new PointBo();
				BeanUtils.copyProperties(pointBo, p);
				list.add(pointBo);
			}
			PageResp<PointBo> pageResp = new PageResp<>();
			pageResp.setList(list);
			pageResp.setPage(page);
			commonResp.success();
			commonResp.setData(pageResp);
		} catch (Exception e) {
			logger.error("获取历史记录发生异常", e);
			commonResp.exception("获取历史记录列表发生异常");
		}

		return commonResp;
	}
}
